import 'package:flutter/material.dart';
import 'package:flutter_default_project/models.dart';
import 'package:flutter_default_project/todo_list_model.dart';
import 'package:provider/provider.dart';

class TodoListView extends StatelessWidget {
  const TodoListView({super.key});

  @override
  Widget build(BuildContext context) {
    return Selector<TodoListModel, List<Todo>>(
      builder: (context, todos, _) {
        return ListView.builder(
          itemBuilder: (context, index) {
            final todo = todos[index];
            return ListTile(
              leading: Checkbox(
                value: todo.complete,
                onChanged: (value) {
                  // todo.complete = value;
                  Provider.of<TodoListModel>(
                    context,
                    listen: false,
                  ).updateTodo(todo.copy(complete: value));
                },
              ),
              title: Text(todo.task),
              subtitle: Text(todo.note),
            );
          },
          itemCount: todos.length,
        );
      },
      selector: (_, model) => model.filteredTodos,
    );
  }
}
